package com.dhcc.cdc.busi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcHospitalMapper;
import com.dhcc.cdc.busi.dao.CdcUserMapper;
import com.dhcc.cdc.busi.entity.CdcHospitalEntity;
import com.dhcc.cdc.busi.entity.CdcUserEntity;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 院区表数据服务层接口
 * 
 * @ClassName: CdcHospitalServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Service("cdcHospitalService")
public class CdcHospitalServiceImpl extends BaseServiceImpl<CdcHospitalMapper, CdcHospitalEntity> implements ICdcHospitalService {
	
	@Autowired
	private CdcUserMapper cdcUserMapper;
	
	@Autowired
	private CdcDataMapper cdcDataMapper;
	
	@Override
	public List<CdcHospitalEntity> page(Page<CdcHospitalEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<CdcHospitalEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public JSONArray getOfficeMap() {
		List<CdcHospitalEntity> cdcHospitalEntities = this.baseMapper.selectList(null);
		JSONArray officeArry = new JSONArray();
		JSONObject obj = new JSONObject();
		obj.put("value", "");
		obj.put("text","全部");
		officeArry.add(obj);
		for(CdcHospitalEntity office : cdcHospitalEntities){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", office.getId() + "");
			officeObj.put("text", office.getDescription());
			officeArry.add(officeObj);
		}
		return officeArry;
	}
	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
        try {
            doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            Iterator<Element> iter = rootElt.elementIterator("hospital"); // 获取根节点下的子节点hospital
            // 遍历hospital节点
            while (iter.hasNext()) {
                Element hospEle = iter.next();
                String xcode= hospEle.elementTextTrim("xcode"); //唯一标识
                String code= hospEle.elementTextTrim("code"); //代码	
                String description= hospEle.elementTextTrim("description"); //描述
                String description2= hospEle.elementTextTrim("description2"); //描述
                String updatetimestr= hospEle.elementTextTrim("updatetime"); //更新时间
                String updateuserid= hospEle.elementTextTrim("updateuserid"); //更新人
                String opercode= hospEle.elementTextTrim("opercode"); //操作码
                CdcHospitalEntity hosp = new CdcHospitalEntity();
                hosp.setXcode(xcode);
                //获取dataid
    			CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
    			cdcDataEntityBase.setCode("HisBase");
    			cdcDataEntityBase.setIsActive(1);
    			cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
        		Long basedataId = cdcDataEntityBase.getId();
        		//获取更新人
            	if(CommonUtil.isNotEmpty(updateuserid)) {
            		CdcUserEntity cdcUserEntity = new CdcUserEntity();
            		cdcUserEntity.setXcode(updateuserid);
            		cdcUserEntity.setDataId(basedataId);
            		cdcUserEntity.setIsActive(1);
            		cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
            		//判断人员存在不存在
            		Long updateuser = null;
            		if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
            			cdcUserEntity = new CdcUserEntity();
            			cdcUserEntity.setXcode(updateuserid);
                		cdcUserEntity.setIsActive(1);
            			cdcUserEntity.setCode("newuser"+updateuserid);
            			cdcUserEntity.setName("newuser"+updateuserid);
                		cdcUserEntity.setDataId(basedataId);
                		//保存用户
                		cdcUserMapper.insert(cdcUserEntity);
                		updateuser = cdcUserEntity.getId();
            		}else {
            			updateuser = cdcUserEntity.getId();
            		}
            		hosp.setUpdateBy(updateuser);
            	}
                if("1".equals(opercode)) {//保存/修改
                	if(CommonUtil.isNotEmpty(code)) {
                		hosp.setCode(code);
                	}
                	if(CommonUtil.isNotEmpty(description)) {
                		hosp.setDescription(description);
                	}
                	if(CommonUtil.isNotEmpty(description2)) {
                		hosp.setDescription2(description2);
                	}
                	if(CommonUtil.isNotEmpty(updatetimestr)) {
                		Date updatetime = sdf.parse(updatetimestr);
                		hosp.setUpdateDate(updatetime);
                	}
            		hosp.setDataId(basedataId);
                	hosp.setIsActive(1);
                }else {
                	hosp.setIsActive(0);
                }
                //先判断是不是有该对象
                Wrapper<CdcHospitalEntity> wrapper =  new EntityWrapper<CdcHospitalEntity>();
                wrapper.eq("xcode", hosp.getXcode());
                wrapper.eq("data_id", basedataId);
                int count = this.selectCount(wrapper);
                if(count>0) {
                	this.update(hosp, wrapper);
                }else {
                	this.insert(hosp);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        } catch (Exception e) {
            e.printStackTrace();
            return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
        }
		return "<response><status>1</status><message>ok</message></response>";
	}

}